package apps;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;

import decoder.ArithmeticDecoder;

import encoder.ArithmeticEncoder;
import filetostring.TextFileToString;

public class Gui extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5825403871734062734L;
	JTextPane textPane;
	JTextArea textArea;
	JTextArea textArea_1;
	JTextArea textArea_2;
	String path;
	String name;
	String text;
	Double sizeBefore;
	Double sizeAfter;
	File f;
	JLabel label;
	JLabel label_1;
	JLabel label_2;
	JLabel label_3;
	JLabel label_4;
	Boolean flag=false;		//if TRUE,use text file is set
	
	public Gui() 
	{
		setResizable(false);//optional
		Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/code2.png"));
		
		setIconImage(img);
		setTitle("Arithmetic Coding");
		getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 664, 144);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		
		//ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("/Ok.png"));
		ImageIcon icon = new ImageIcon(getClass().getResource("/Ok.png"));
		
		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnBrowse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFileChooser chooser = new JFileChooser();
			    chooser.setCurrentDirectory(new File("."));

			    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
			      public boolean accept(File file) 
			      {
			        return file.getName().toLowerCase().endsWith(".txt")
			        		|| file.getName().toLowerCase().endsWith(".bin")
			            || file.isDirectory();
			      }
			      public String getDescription() {
			        return "Text file,Binary file";
			      }
			    });
			    int r = chooser.showOpenDialog(new JFrame());
			    if (r == JFileChooser.APPROVE_OPTION) {
			    	
			    name = chooser.getSelectedFile().getPath();
			    textPane.setText(name);
			    
			    f = new File(chooser.getSelectedFile().getPath());
			    TextFileToString temp = new TextFileToString(f);
			    
				label_1.setText(Long.toString(f.length())+"byte");
				//temp contains text of input file,show it in text area
				if(f.getName().toLowerCase().endsWith(".txt"))
					textArea.setText(temp.getStr());
				sizeBefore= new Double(f.length());
			    //text file was selected
			    flag=true;
			    }
			}
		});
		btnBrowse.setBounds(555, 13, 97, 25);
		panel.add(btnBrowse);
		
		textPane = new JTextPane();
		textPane.setFont(new Font("Tahoma", Font.PLAIN, 14));
		textPane.setEditable(false);
		textPane.setBounds(126, 16, 398, 22);
		panel.add(textPane);
		
		JLabel lblSelectTextFile = new JLabel("Select text file");
		lblSelectTextFile.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSelectTextFile.setBounds(12, 22, 102, 16);
		panel.add(lblSelectTextFile);
		
		//Encode
		JButton btnEncode = new JButton("Encode");
		btnEncode.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{	
				//text file selected
				if(flag==true && f.getName().toLowerCase().endsWith(".txt"))
				{
					
						try {
							new ArithmeticEncoder(new FileInputStream(f),getFilePath());
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
							
						label.setVisible(true);
						
						//show compressed text
						TextFileToString temp = new TextFileToString(new File(getFilePath()+"BitSequens.txt"));
						textArea_1.setText(temp.getStr());
						//compressed binary file size
						label_2.setText(Long.toString(new File(getFilePath()+"compressed.bin").length())+"byte");
						///////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
						f = new File(getFilePath()+"compressed.bin");
						sizeAfter = new Double(f.length());
						double res = (sizeAfter/sizeBefore)*100;
						label_4.setText(new DecimalFormat("##.##").format(res)+"%");
						flag=false;
			
				}
				//text file wasn't selected
				else
				{
					String message = "Nothing to encode!!!\n" +
							"Select input text file.";
					    JOptionPane.showMessageDialog(new JFrame(), message, "Error",
					        JOptionPane.ERROR_MESSAGE);
				}
			}	
		});
		btnEncode.setBounds(446, 51, 97, 25);
		panel.add(btnEncode);
		
		JButton buttonDec = new JButton("Decode");
		buttonDec.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				if( f.getName().toLowerCase().endsWith(".bin"))
				{
					try {
						new ArithmeticDecoder(new FileInputStream(new File(getFilePath()+"compressed.bin")),getFilePath());
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					label_3.setVisible(true);
					
					TextFileToString temp = new TextFileToString(new File(getFilePath()+"decompressed.txt"));
					textArea_2.setText(temp.getStr());
					
					flag=false;
				}
				else
				{
					String message = "Nothing to encode!!!\n" +
							"Select input text file.";
					    JOptionPane.showMessageDialog(new JFrame(), message, "Error",
					        JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		buttonDec.setBounds(555, 51, 97, 25);
		panel.add(buttonDec);
		
		label = new JLabel(icon);
		label.setBounds(476, 83, 48, 48);
		panel.add(label);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) //clear all fields
			{
				textArea.setText("");
				textArea_1.setText("");
				textArea_2.setText("");
				textPane.setText("");
				label_2.setText("");
				label_1.setText("");
				label.setVisible(false);
				label_3.setVisible(false);
				label_4.setText("");
			}
		});
		
		btnClear.setBounds(337, 51, 97, 25);
		panel.add(btnClear);
		
		
		
		label_3 = new JLabel(icon);
		label_3.setBounds(575, 83, 62, 48);
		panel.add(label_3);
		label_3.setVisible(false);
		
		label.setVisible(false);
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 146, 453, 455);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(12, 24, 439, 421);
		panel_1.add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		scrollPane.setViewportView(textArea);
		
		JLabel lblInputText = new JLabel("Input Text:");
		lblInputText.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblInputText.setBounds(12, 0, 77, 29);
		panel_1.add(lblInputText);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(465, 146, 448, 455);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(0, 24, 446, 420);
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		panel_2.add(scrollPane_1);
		
		textArea_1 = new JTextArea();
		textArea_1.setLineWrap(true);
		scrollPane_1.setViewportView(textArea_1);
		
		JLabel lblCompressedText = new JLabel("Compressed text:");
		lblCompressedText.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCompressedText.setBounds(0, 0, 124, 29);
		panel_2.add(lblCompressedText);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBounds(675, 0, 679, 144);
		getContentPane().add(panel_3);
		panel_3.setLayout(null);
		
		JLabel lblStatistics = new JLabel("Statistics");
		lblStatistics.setBounds(0, 0, 72, 24);
		panel_3.add(lblStatistics);
		lblStatistics.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblInputFileSize = new JLabel("Input file size:");
		lblInputFileSize.setBounds(0, 26, 95, 16);
		panel_3.add(lblInputFileSize);
		lblInputFileSize.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblBinaryFileSize = new JLabel("Binary file size:");
		lblBinaryFileSize.setBounds(0, 48, 95, 16);
		panel_3.add(lblBinaryFileSize);
		lblBinaryFileSize.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		label_1 = new JLabel("");
		label_1.setBounds(137, 26, 72, 16);
		panel_3.add(label_1);
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		label_2 = new JLabel("");
		label_2.setBounds(137, 48, 72, 16);
		panel_3.add(label_2);
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblCompressionRatio = new JLabel("Compression ratio:");
		lblCompressionRatio.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCompressionRatio.setBounds(0, 75, 127, 16);
		panel_3.add(lblCompressionRatio);
		
		label_4 = new JLabel("");
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
		label_4.setBounds(137, 76, 72, 16);
		panel_3.add(label_4);
		
		JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBounds(912, 146, 442, 455);
		getContentPane().add(panel_4);
		
		JLabel lblDecompressedText = new JLabel("Decompressed text:");
		lblDecompressedText.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDecompressedText.setBounds(12, 0, 148, 29);
		panel_4.add(lblDecompressedText);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_2.setBounds(12, 24, 428, 418);
		panel_4.add(scrollPane_2);
		
		textArea_2 = new JTextArea();
		scrollPane_2.setViewportView(textArea_2);
		textArea_2.setLineWrap(true);
		
	}
	public String getFilePath()//get parent directory
	{
		String path;
		path=f.getParent()+"/";
		
		return path;
	}
}
